﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Threading.Tasks;
using MVVMCommon.ViewModels.Commands;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
namespace FlashlightRecorder.ViewModels
{
    public class Player_Model : DisposableHost,INotifyPropertyChanged
    {
        public Repositories.SimpleRepository<Record> RecordRepository { get; set; }
        public ReactiveCommand StartStopCommand { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public Player_Model()
        {
            //OpacityAnimation = new DoubleAnimationUsingKeyFrames();
            //PlayCommand = new ReactiveCommand(false);
            //PauseCommand = new ReactiveCommand(false);
            //PauseCommand = new ReactiveCommand(false);
            _loopTime = new RepeatBehavior(1);
            StartStopCommand = new ReactiveCommand(true);
            var rcd = Observable.FromEvent<PropertyChangedEventHandler, PropertyChangedEventArgs>(
                             ac => (s, e) => ac(e),
                             eh => this.PropertyChanged += eh,
                             eh => this.PropertyChanged -= eh
                         )
                         .Where(x => x.PropertyName == "CurrentRecord");


            rcd.Select(_ => true) //this.CurrentRecord != null)
                .Subscribe(StartStopCommand);

            StartStopCommand.Subscribe(
                    targetSB =>
                    {
                        //this.CurrentRecord = App.ViewModel.RecorderModel.RecordingTarget;
                        var sb = targetSB as Storyboard;
                        if (sb.GetCurrentState() == ClockState.Stopped || sb.GetCurrentState() == ClockState.Filling)
                        {
                            if (CurrentRecord!=null)
                            {
                                
                       
                            sb.Stop();
                            var kfs = CurrentRecord.Items
                            .SelectMany(
                                itm =>
                                {
                                    var k1 = new EasingDoubleKeyFrame() { Value = 0, KeyTime = itm.Start, EasingFunction = _easingFunction };
                                    var k2 = new EasingDoubleKeyFrame() { Value = 1, KeyTime = itm.End, EasingFunction = _easingFunction };
                                    return new[] { k1, k2 };
                                }
                            )
                            .ToList();
                            kfs.Add(new EasingDoubleKeyFrame() { Value = 0, KeyTime = CurrentRecord.Duration, EasingFunction = _easingFunction });
                            var ani = sb.Children.First() as DoubleAnimationUsingKeyFrames;
                            ani.KeyFrames.Clear();
                            kfs
                                .ForEach(x => ani.KeyFrames.Add(x));
                            

                            sb.Begin();
                            }
                        }
                        else
                        {
                            sb.Stop();
                        }


                    }

                );


        }


        //public ReactiveCommand PlayCommand { get; private set; }
        //public ReactiveCommand PauseCommand { get; private set; }
        //public ReactiveCommand StopCommand { get; private set; }


        //DoubleAnimationUsingKeyFrames _opacityAnimation;


        //public DoubleAnimationUsingKeyFrames OpacityAnimation
        //{
        //    get { return _opacityAnimation; }
        //    set { _opacityAnimation = value; NotifyPropertyChanged("OpacityAnimation"); }
        //}

        private IEasingFunction _easingFunction = new QuinticEase { EasingMode = EasingMode.EaseOut };





















        Record _currentRecord;

        public Record CurrentRecord
        {
            get { return _currentRecord; }
            set
            {


                _currentRecord = value;
                NotifyPropertyChanged("CurrentRecord");
            }
        }


        bool _loop;

        public bool Loop
        {
            get { return _loop; }
            set
            {
                _loop = value;
                NotifyPropertyChanged("Loop");
                LoopTime = value ? RepeatBehavior.Forever: once;
            }
        }

        RepeatBehavior once = new RepeatBehavior(1);
        

        RepeatBehavior _loopTime;

        public RepeatBehavior LoopTime
        {
            get { return _loopTime; }
            set
            {
                _loopTime = value;
                NotifyPropertyChanged("LoopTime");
            }
        }










    }


}
